package com.example.data;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;



import android.util.Log;
import android.util.Xml;

public class ParseData {

	
	public StockList parseXML(String xml) {
		StockList stockList = new StockList();
		XmlPullParser parser = Xml.newPullParser();
		try {			
		/*	if(xml == null)
				return ;*/
			parser.setInput(new StringReader(xml));
			int event = parser.getEventType();
			
			
			stockList.records = new ArrayList<StockList.Record>();
			StockList.Record record = null;
			try {
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_TAG:
						
						String tagName = parser.getName();
						
						
						if("item".equals(tagName)) {
							record = new StockList.Record();
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								stockList.records.add(record);
								//parser.nextTag();
							}
						} else if ("code".equals(tagName)) {
							String tagText = parser.nextText();
							record.setCode(Integer.parseInt(tagText));
							
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}
						} else if ("name".equals(tagName)) {
							String tagText = parser.nextText();
							record.setName(tagText);
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}
						} else if ("currentvalue".equals(tagName)) {
							String tagText = parser.nextText();
							record.setCurrent(Float.parseFloat(tagText));
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}
						} else if ("symbol".equals(tagName)) {
							String tagText = parser.nextText();
							record.setSymbol(Integer.parseInt(tagText));
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}
						} else if ("fluctuation".equals(tagName)) {
							String tagText = parser.nextText();
							record.setFluc(tagText);
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}
						} else if ("high".equals(tagName)) {
							String tagText = parser.nextText();
							record.setHigh(Float.parseFloat(tagText));
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}
						} else if ("low".equals(tagName)) {
							String tagText = parser.nextText();
							record.setLow(Float.parseFloat(tagText));
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}
						} else if ("settlement".equals(tagName)) {
							String tagText = parser.nextText();
							record.setSettlement(Float.parseFloat(tagText));
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}							
						} else if ("day".equals(tagName)) {
							String tagText = parser.nextText();
							//Log.e("supersky", "day = "+tagText);
							stockList.setDay(Integer.parseInt(tagText));
							if (parser.getEventType() != XmlPullParser.END_TAG) {
								parser.nextTag();
							}							
						}
						
						
						
					case XmlPullParser.END_TAG:
						break;
					}
					try {
						event = parser.next();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (NullPointerException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return stockList;
	}
}
